<?php
class core_time extends object {

public $date="";
public $time="";
public $timestamp="";

//mktime(hour, minute, second, month, day, year)
function verify_values() {
	$loc = new loc;
	$out="";	
	if ($this->date == "") {
	$out=$out."<br>".$loc->time_date_miss; 
		}
	if ($this->time == "") {
	$out=$out."<br>".$loc->time_time_miss; 
		}
	return $out;
}



//spoji datumovu a casovu cast vstupu
function glue_time_date() {
	
	
	}
//skontroluje ci string na vstupe je mozne skonvertit do time
function verify_time_format() {
	$loc = new loc;
	$out="";		
	$verification=$this->verify_values();
	if ($verification == "") {
	if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}$/', $this->time) == 0) {
		$out=$out."<br>".$loc->time_time_missmatch;
		
		} else {
		$times = explode(":", $this->time);
		if ($times[0] > 24 OR $times[0] < 0 OR $times[1] > 60 OR $times[1] < 0) {
			$out=$out."<br>".$loc->time_time_outofboundries;
			}		
		}
	
	
	} else {
	$out=$out."<br>".$verification;	
	}
return $out; 	
}

function verify_date_format() {
	$loc = new loc;
	$out="";		
	$verification=$this->verify_values();
	if ($verification == "") {
		if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{4}$/', $this->date) == 0) {
			$out=$out."<br>".$loc->time_date_missmatch;
			} else {
			$dates = explode(":", $this->date);
			if ($dates[0] > 31 OR $dates[0] < 0 OR $dates[1] > 12 OR $dates[1] < 0) {
			$out=$out."<br>".$loc->time_date_outofboundries;
				}		
			}		
		} else {
		$out=$out."<br>".$verification;			
		}
	return $out; 
	}
	
function verify_date_format_wotime() {
	$loc = new loc;
	$out="";		
	if ($this->date != "") {
		if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{4}$/', $this->date) == 0) {
			$out=$out."<br>".$loc->time_date_missmatch;
			} else {
			$dates = explode(":", $this->date);
			if ($dates[0] > 31 OR $dates[0] < 0 OR $dates[1] > 12 OR $dates[1] < 0) {
			$out=$out."<br>".$loc->time_date_outofboundries;
				}		
			}		
		} else {
					
		}
	return $out; 
	}
	
function verify_time_format_wodate() {
	$loc = new loc;
	$out="";		
	if ($this->time != "") {
	if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}$/', $this->time) == 0 AND preg_match('/^[0-9]{1,2}h[0-9]{1,2}m$/', $this->time) == 0 AND preg_match('/^[0-9]{1,2}h$/', $this->time) == 0 AND preg_match('/^[0-9]{1,2}m$/', $this->time) == 0) {
		$out=$out."<br>".$loc->time_time_missmatch;
		
		} else {
		if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}$/', $this->time)) {
		$times = explode(":", $this->time);
		if ($times[0] > 24 OR $times[0] < 0 OR $times[1] > 60 OR $times[1] < 0) {
			$out=$out."<br>".$loc->time_time_outofboundries;
			}		
		}
		if (preg_match('/^[0-9]{1,2}h[0-9]{1,2}m$/', $this->time)) {
		$times = explode("h", $this->time);
		if ($times[0] > 24 OR $times[0] < 0 OR $times[1] > 60 OR $times[1] < 0) {
			$out=$out."<br>".$loc->time_time_outofboundries;
			}		
		}
		if (preg_match('/^[0-9]{1,2}h$/', $this->time)) {
		$times = explode("h", $this->time);
		if ($times[0] > 24 OR $times[0] < 0) {
			$out=$out."<br>".$loc->time_time_outofboundries;
			}		
		}
		if (preg_match('/^[0-9]{1,2}m$/', $this->time)) {
		$times = explode("m", $this->time);
		if ($times[0] < 0) {
			$out=$out."<br>".$loc->time_time_outofboundries;
			}		
		}
		
		}
	
	
	} else {}
return $out; 	
}

//(hour, minute, second, month, day, year)
function totimestamp() {
	$verification=$this->verify_time_format().$this->verify_date_format();
	if ($verification == "") {
		//mktime(hour, minute, second, month, day, year)
		$times = explode(":", $this->time);
		$dates = explode(":", $this->date);
		return mktime($times[0],$times[1],00,$dates[1],$dates[0],$dates[2]);		
		}	
	}

//function that returns from, to, or duration time computed from other two parameters
//$action determines what should be computed from what. "to" compute ending time from starting time and duration,"duration" compute duration from starting and ending time.
//in case function "duration" is used and user dont have right to edit duration, it computes starting time from ending time and duration
//function requires presence of login session variables and therefore have to be called after login verification   
function computetime($timefrom,$timeto,$duration,$action) {
	$this->date=$_SESSION['date2'];
	$this->time=$timefrom;
	$timefrom=$this->totimestamp();
	$this->time=$timeto;
	$timeto=$this->totimestamp();
	$duration=$this->tosec($duration);
	//return $duration;
	if ($action == "to") {
		$timeto=$timefrom+$duration;
		return $this->totimedef($timeto);
		//return $timefrom." ".$timeto." ".$duration;		
		}
	if ($action == "duration" AND $_SESSION['role'] == "3") {
		//computing starting time from ending time and duration
		$timefrom=$timeto-duration;
		return $this->totimedef($timefrom);		
		} else {
		//computing duration from starting and ending time
		$duration=$timeto-$timefrom;
		return $this->tohm($duration);
		//return $duration." timeto:".$timeto." timefrom:".$timefrom;
		}
	}
	


function tosec($time) {
	if (preg_match('/^[0-9]{1,2}:[0-9]{1,2}$/', $time)) {
		$times = explode(":", $time);
		return $times[0]*60*60+$times[1]*60;	
		} 
	
	if (preg_match('/^[0-9]{1,2}h$/', $time)) {
		$times = explode("h", $time);
		return $times[0]*60*60;	
		}
	if (preg_match('/^[0-9]{1,2}m$/', $time)) {
		$times = explode("m", $time);
		return $times[0]*60;	
		}
	if (preg_match('/^[0-9]{1,2}h[0-9]{1,2}m$/', $time)) {
		$times = explode("h", $time);
		$minutes = explode("m", $times[1]);
		return $times[0]*60*60+$minutes[0]*60;
		} 
	
	}

function tohm($seconds) {
	$minutes=(int)($seconds/60);
	$hours=(int)($minutes/60);
	$minutes=$minutes-$hours*60;
	if ($minutes < 10) {$minutes="0".$minutes;}
	return $hours.":".$minutes;	
	}

function todate() {
return date('d-m-Y',$this->timestamp);
}

function todatedef($tstamp) {
return date('d-m-Y',$tstamp);
}

function todatedef_separator($tstamp,$separator) {
return date($separator,$tstamp);
}

function totime() {
return date('H:i',$this->timestamp);
}

function totimedef($tstamp) {
return date('H:i',$tstamp);
}

function totimestampdef($date,$time) {
$this->time=$time;
$this->date=$date;
return $this->totimestamp();

}

}

?>
